package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.annotation.LogAnno;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.text.ParseException;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;

    @LogAnno(operateType = "预约列表分页查询")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            return orderService.findPage(queryPageBean);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
   @LogAnno(operateType = "新增预约")
    @RequestMapping("/add")
    public Result add(@RequestBody Map map) {
        map.put("orderType", "电话预约");
        String telephone = (String) map.get("phoneNumber");
        Result result = null;
        try {
            result = orderService.order(map);//通过Dubbo远程调用服务实现在线预约业务处理
            if (result.isFlag()) {
                //预约成功，可以为用户发送短信
                /*try {
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, (String) map.get("orderDate"));
                } catch (Exception e) {
                    e.printStackTrace();
                    return new Result(false, "新增预约失败");
                }*/
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增预约失败");
        }
    }
    @LogAnno(operateType = "开启体检")
    @RequestMapping("/open")
    public Result open(@RequestBody Map map){
        Result result = orderService.open(map);
        return result;
    }
    @LogAnno(operateType = "取消预约")
    @RequestMapping("/cancel")
    public Result cancel(@RequestBody Map<String,String> map){
        Result result = orderService.cancel(map);
        return result;
    }

}
